package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.SetmealService;
import com.sky.utils.TextAutoRouteUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageBean queryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.queryPage(setmealPageQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());
    }

    /**
     * 添加新套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insert(setmealDishes);
        }

    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            setmealDishMapper.deleteById(id);
        }
        setmealMapper.deleteByIds(ids);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @SneakyThrows
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //判断描述是否合法
//        if(setmeal.getDescription()!=null||setmeal.getDescription()!=""){
//            String textAutoRoute = TextAutoRouteUtil.textAutoRoute(setmeal.getDescription());
//            if(!Objects.equals(textAutoRoute, "")) {
//                throw new BaseException(textAutoRoute);
//            }
//        }

        setmealMapper.updateSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //添加套餐菜品信息
        setmealDishMapper.deleteById(setmealDTO.getId());
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insert(setmealDishes);
        }

    }

    /**
     * 根据id查套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal, setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 套餐起售、停售
     *
     * @param status
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        //如果是起售
        if (status.equals(StatusConstant.ENABLE)) {
            //只要有一个未起售菜品就抛异常
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            List<Dish> dishes=new ArrayList<>();
            for (SetmealDish setmealDish : setmealDishes) {
                Dish dish = dishMapper.getById(setmealDish.getDishId());
                if (dish.getStatus().equals(StatusConstant.DISABLE))
                    throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmeal.setStatus(status);
        setmealMapper.updateSetmeal(setmeal);
    }



    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmeals = setmealMapper.getByCateId(setmeal.getCategoryId());
        return setmeals;
    }

    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishBySetmealId(Long id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        List<DishItemVO> dishItemVOs=new ArrayList<>();
        setmealDishes.forEach(setmealDish -> {
            Dish dish = dishMapper.getById(setmealDish.getDishId());
            DishItemVO dishItemVO=new DishItemVO();
            BeanUtils.copyProperties(dish,dishItemVO);
            dishItemVO.setCopies(setmealDish.getCopies());
            dishItemVOs.add(dishItemVO);
        });
        return dishItemVOs;
    }

}
